package com.jhg.drgclinicalpathway;

import com.jhg.drgclinicalpathway.dao.PathTplDao;
import com.jhg.drgclinicalpathway.domain.PathTpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 创建文件上传目录
 */
@Component
@Slf4j
public class CreateUploadDir  implements CommandLineRunner {

    @Value("${upload-path}")
    private String uploadPath;

    @Autowired
    PathTplDao pathTplDao;
    @Override
    public void run(String... args) throws Exception {
        String f=uploadPath+"/files";
        if(!Files.exists(Paths.get(f))){
            File file = new File(f);
            boolean result = file.mkdirs();
            if(result){
                log.info("创建文件上传目录成功："+f);
            }
        }
        List<Path> fileLs = Files.list(Paths.get(f)).collect(Collectors.toList());
        for (Path l : fileLs) {
            String s = l.getFileName().toString()
                    .replaceAll("-路径方案", "")
                    .replaceAll("-路径表单", "");
            PathTpl tpl = pathTplDao.selectById(s);
            if(tpl==null) continue;
            tpl.setUpload(true);
            pathTplDao.updateById(tpl);
        }
    }
}
